package com.zhongjian.technologycal.mall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.bean.HomeBean;

import java.util.List;

/**
 * Created by wei on 2018/1/15.
 * 首页数据的适配器
 * 主要作为适配每个item的数据
 */

public class HomeGridViewAdapter extends BaseAdapter {
    LayoutInflater inflater = null;
    List<HomeBean> listInfo;
    Context context;

    public HomeGridViewAdapter(Context context, List<HomeBean> listInfo) {
        inflater = LayoutInflater.from(context);
        this.listInfo = listInfo;
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listInfo.size();
    }

    @Override
    public Object getItem(int index) {
        // TODO Auto-generated method stub
        return listInfo.get(index);
    }

    @Override
    public long getItemId(int index) {
        // TODO Auto-generated method stub
        return index;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null) {
            convertView = inflater.inflate(R.layout.current_list, null);
            holder = new ViewHolder();
            holder.home_iv_product_photo = convertView.findViewById(R.id.home_iv_product_photo);
            holder.home_tv_price = convertView.findViewById(R.id.home_tv_price);
            holder.home_tv_product_name = convertView.findViewById(R.id.home_tv_product_name);
            holder.home_tv_credit = convertView.findViewById(R.id.home_tv_credit);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        HomeBean appInfo = listInfo.get(position);
        holder.home_tv_price.setText("￥" + appInfo.getData().getProducts().get(1).getProductsOfTag().get(position).getPrice());
        holder.home_tv_product_name.setText(appInfo.getData().getProducts().get(1).getProductsOfTag().get(position).getProductname());
        Picasso.get()
                .load(BaseContacts.BASE_URL+ appInfo.getData().getProducts().get(1).getProductsOfTag().get(position).getProductphotos().get(0).getPhoto())
                .into(holder.home_iv_product_photo);
        holder.home_tv_credit.setText(""+ appInfo.getData().getProducts().get(1).getProductsOfTag().get(position).getElecnum() / 2);

        return convertView;
    }

    public class ViewHolder {
        ImageView home_iv_product_photo;
        TextView home_tv_price;
        TextView home_tv_product_name;
        TextView home_tv_credit;
    }
}
